// 引入数据库
const conn = require('../../db');
const router = require('express').Router();

// 获取数据
router.get('/recruit', (req, res) => {
    // 规定pagenum  当前页数  默认为  1
    pagenum = req.query.pagenum || 1;
    // 开始下标
    startIndex = (pagenum - 1) * 12;
    // sql
    let sql = `select * from it_job limit ${startIndex},12; select count(*) total from it_job `;
    conn.query(sql, (err, data) => {
        if (err) return console.log(err);
        // console.log(data);
        res.json({
            code: 200,
            data: data[0],
            total: data[1][0].total
        })
    })
})

// 检索
router.get('/recruit/serch', (req, res) => {
    // 工资规定
    // console.log(req.query);//
    // sql = 'select count(*) from it_job where assort=?;select count(*) from it_job where area=?;select count(*) from it_job where wages=?;select count(*) from it_job where task=?'
    sql = `SELECT * from it_job where assort like "%${req.query.assort}%" limit 12;`
    conn.query(sql, (err, data) => {
        if (err) return console.log(err);
        res.json({
            code: 200,
            data: data
        })
    })
})

// 登录
router.post('/recruit/login', (req, res) => {
    let sql = 'select * from it_user where email=?';
    let email = req.body.email;
    let password = req.body.password;
    // let password = req.body.password
    conn.query(sql, [email, password], (err, data) => {
        if (err) return console.log(err);
        if (data != 0) {
            res.json({
                code: 200,
                message: '登录成功',
                data: data
            })
        } else {
            res.json({
                code: 400,
                message: '请输入正确的邮箱密码'
            })
        }
    })

})


// 注册
router.post('/recruit/zhuce', (req, res) => {
    let username = req.body.username;
    let email = req.body.email;
    let password = req.body.password;
    let sql = 'insert into it_user (username,email,password)values(?,?,?) ';
    conn.query(sql, [username, email, password], (err, data) => {
        if (err) return console.log(err);
        console.log(data)
        if (data != 0) {
            res.json({
                code: 200,
                message: '注册成功',
                data: data
            })
        } else {
            res.json({
                code: 400,
                message: '请填写信息'
            })
        }
    })

})



// /暴露路由
module.exports = router;